package com.lefevre.cms.service.favorite.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lefevre.cms.bean.QueryResult;
import com.lefevre.cms.bean.favorite.Favorites;
import com.lefevre.cms.bean.favorite.QuestionFavorite;
import com.lefevre.cms.bean.favorite.TopicFavorite;
import com.lefevre.cms.mapper.FavoritesMapper;
import com.lefevre.cms.mapper.QuestionFavoriteMapper;
import com.lefevre.cms.mapper.TopicFavoriteMapper;
import com.lefevre.cms.service.besa.DaoSupport;
import com.lefevre.cms.service.favorite.FavoriteService;
import net.sf.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 收藏夹
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FavoriteServiceBean extends DaoSupport<Favorites> implements FavoriteService {

    @Resource
    private FavoritesMapper favoritesMapper;
    @Resource
    private TopicFavoriteMapper topicFavoriteMapper;
    @Resource
    private QuestionFavoriteMapper questionFavoriteMapper;

    /**
     * 根据Id查询收藏夹
     *
     * @param favoriteId 收藏夹Id
     * @return 收藏对象
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Favorites findById(String favoriteId) {
        return favoritesMapper.selectById(favoriteId);
    }

    /**
     * 根据用户名称查询收藏夹分页
     *
     * @param userId     用户Id
     * @param userName   用户名称
     * @param firstIndex 索引开始,即从哪条记录开始
     * @param maxResult  获取多少条数据
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public QueryResult<Favorites> findFavoriteByUserId(Long userId, String userName, int firstIndex, int maxResult) {
        QueryResult<Favorites> qr = new QueryResult<>();

        LambdaQueryWrapper<Favorites> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Favorites::getUserName, userName);
        wrapper.orderByDesc(Favorites::getAddtime);
        Page<Favorites> favoritesPage = favoritesMapper.selectPage(new Page<>(firstIndex, maxResult), wrapper);

        qr.setResultlist(favoritesPage.getRecords());
        qr.setTotalrecord(favoritesPage.getTotal());
        return qr;
    }


    /**
     * 保存收藏
     *
     * @param favorites        收藏夹
     * @param topicFavorite    话题收藏
     * @param questionFavorite 问题收藏
     */
    public void saveFavorite(Object favorites, Object topicFavorite, Object questionFavorite) {
        favoritesMapper.insert((Favorites) favorites);

        if (topicFavorite != null) {
            topicFavoriteMapper.insert((TopicFavorite) topicFavorite);
        }
        if (questionFavorite != null) {
            questionFavoriteMapper.insert((QuestionFavorite) questionFavorite);
        }
    }

    /**
     * 删除收藏
     *
     * @param favoriteId         收藏夹Id
     * @param topicFavoriteId    话题收藏Id
     * @param questionFavoriteId 问题收藏Id
     */
    public Integer deleteFavorite(String favoriteId, String topicFavoriteId, String questionFavoriteId) {
        int i = favoritesMapper.deleteById(favoriteId);
        if (topicFavoriteId != null) {
            i += topicFavoriteMapper.deleteById(topicFavoriteId);
        }
        if (questionFavoriteId != null) {
            i += questionFavoriteMapper.deleteById(questionFavoriteId);
        }
        return i;
    }

    /**------------------------------------------- 话题 ------------------------------------------------**/


    /**
     * 根据话题Id删除收藏
     *
     * @param topicId 话题Id
     */
    public Integer deleteFavoriteByTopicId(Long topicId) {
        int j = favoritesMapper.delete(new LambdaUpdateWrapper<Favorites>().eq(Favorites::getTopicId, topicId));
        j += topicFavoriteMapper.delete(new LambdaUpdateWrapper<TopicFavorite>().eq(TopicFavorite::getTopicId, topicId));
        return j;
    }

    /**
     * 根据收藏夹用户名称删除收藏
     *
     * @param userNameList 用户名称集合
     */
    public Integer deleteFavoriteByUserName(List<String> userNameList) {
        int j = favoritesMapper.delete(new LambdaUpdateWrapper<Favorites>().in(Favorites::getUserName, userNameList));
        j += topicFavoriteMapper.delete(new LambdaUpdateWrapper<TopicFavorite>().in(TopicFavorite::getUserName, userNameList));
        j += questionFavoriteMapper.delete(new LambdaUpdateWrapper<QuestionFavorite>().in(QuestionFavorite::getUserName, userNameList));
        return j;
    }

    /**
     * 根据发布话题的用户名称删除收藏
     *
     * @param userNameList 发布话题的用户名称集合
     */
    public Integer deleteFavoriteByPostUserName(List<String> userNameList) {
        int j = favoritesMapper.delete(new LambdaUpdateWrapper<Favorites>().in(Favorites::getPostUserName, userNameList));
        j += topicFavoriteMapper.delete(new LambdaUpdateWrapper<TopicFavorite>().in(TopicFavorite::getPostUserName, userNameList));
        j += questionFavoriteMapper.delete(new LambdaUpdateWrapper<QuestionFavorite>().in(QuestionFavorite::getPostUserName, userNameList));
        return j;
    }


    /**
     * 根据Id查询话题收藏
     *
     * @param topicFavoriteId 话题收藏Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public TopicFavorite findTopicFavoriteById(String topicFavoriteId) {
        return topicFavoriteMapper.selectById(topicFavoriteId);
    }


    /**
     * 根据话题Id查询收藏夹分页
     *
     * @param firstIndex 索引开始,即从哪条记录开始
     * @param maxResult  获取多少条数据
     * @param topicId    话题Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public QueryResult<Favorites> findFavoritePageByTopicId(int firstIndex, int maxResult, Long topicId) {
        QueryResult<Favorites> qr = new QueryResult<>();

        LambdaQueryWrapper<TopicFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TopicFavorite::getTopicId, topicId);
        wrapper.orderByDesc(TopicFavorite::getAddtime);
        Page<TopicFavorite> topicFavoritePage = topicFavoriteMapper.selectPage(new Page<>(firstIndex, maxResult), wrapper);

        List<TopicFavorite> topicFavoriteList = topicFavoritePage.getRecords();
        List<Favorites> favoritesList = new ArrayList<>();
        if (topicFavoriteList != null && topicFavoriteList.size() > 0) {
            BeanCopier copier = BeanCopier.create(TopicFavorite.class, Favorites.class, false);
            for (TopicFavorite topicFavorite : topicFavoriteList) {
                Favorites favorites = new Favorites();
                copier.copy(topicFavorite, favorites, null);
                favoritesList.add(favorites);
            }
        }
        qr.setResultlist(favoritesList);
        qr.setTotalrecord(topicFavoritePage.getTotal());
        return qr;
    }


    /**
     * 根据话题Id查询被收藏数量
     *
     * @param topicId 话题Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Long findFavoriteCountByTopicId(Long topicId) {
        return topicFavoriteMapper.selectCount(new LambdaQueryWrapper<TopicFavorite>().eq(TopicFavorite::getTopicId, topicId));
    }


    /**------------------------------------------- 问题 ------------------------------------------------**/

    /**
     * 根据问题Id删除收藏
     *
     * @param questionId 问题Id
     */
    public Integer deleteFavoriteByQuestionId(Long questionId) {
        int j = favoritesMapper.delete(new LambdaUpdateWrapper<Favorites>().eq(Favorites::getQuestionId, questionId));
        j += questionFavoriteMapper.delete(new LambdaUpdateWrapper<QuestionFavorite>().eq(QuestionFavorite::getQuestionId, questionId));
        return j;
    }


    /**
     * 根据Id查询问题收藏
     *
     * @param questionFavoriteId 问题收藏Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public QuestionFavorite findQuestionFavoriteById(String questionFavoriteId) {
        return questionFavoriteMapper.selectById(questionFavoriteId);
    }

    /**
     * 根据问题Id查询收藏夹分页
     *
     * @param firstIndex 索引开始,即从哪条记录开始
     * @param maxResult  获取多少条数据
     * @param questionId 问题Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public QueryResult<Favorites> findFavoritePageByQuestionId(int firstIndex, int maxResult, Long questionId) {
        QueryResult<Favorites> qr = new QueryResult<>();

        LambdaQueryWrapper<QuestionFavorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionFavorite::getQuestionId, questionId);
        wrapper.orderByDesc(QuestionFavorite::getAddtime);
        Page<QuestionFavorite> questionFavoritePage = questionFavoriteMapper.selectPage(new Page<>(firstIndex, maxResult), wrapper);

        List<QuestionFavorite> questionFavoriteList = questionFavoritePage.getRecords();
        List<Favorites> favoritesList = new ArrayList<>();
        if (questionFavoriteList != null && questionFavoriteList.size() > 0) {
            BeanCopier copier = BeanCopier.create(QuestionFavorite.class, Favorites.class, false);
            for (QuestionFavorite questionFavorite : questionFavoriteList) {
                Favorites favorites = new Favorites();
                copier.copy(questionFavorite, favorites, null);
                favoritesList.add(favorites);
            }
        }
        qr.setResultlist(favoritesList);
        qr.setTotalrecord(questionFavoritePage.getTotal());
        return qr;
    }

    /**
     * 根据问题Id查询被收藏数量
     *
     * @param questionId 问题Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public Long findFavoriteCountByQuestionId(Long questionId) {
        return questionFavoriteMapper.selectCount(new LambdaQueryWrapper<QuestionFavorite>().eq(QuestionFavorite::getQuestionId, questionId));
    }

}
